/**
 * 
 */
package cn.ivicar.app.controller.service;

import java.util.List;

import com.google.inject.Inject;

import cn.com.carsmart.exception.CarsmartException;
import cn.com.carsmart.ws.ibatis.model.BaseExample;
import cn.com.carsmart.ws.ibatis.model.BaseExample.Criteria;
import cn.com.carsmart.ws.model.CustomerVehicle;
import cn.com.carsmart.ws.model.Vehicle;
import cn.com.carsmart.ws.service.CustomerVehicleManager;

/**
 * 管理CustomerVehicle表信息
 * @author liurunbin 2017年5月18日 cn.ivicar.app.controller.service
 */
public class CustomerVehicleService {

	@Inject
	private CustomerVehicleManager cvManager;
    /*
    依据顾客id和车辆id封装成list
     */
	public CustomerVehicle get(Long customerId, Long vehicleId) {
		CustomerVehicle cv = new CustomerVehicle();
		cv.setCustomerId(customerId);
		cv.setVehicleId(vehicleId);
		List<CustomerVehicle> list = cvManager.listByExample(cv);
		if (list == null || list.isEmpty()) {
			return null;
		}

		return list.get(0);
	}
    /*
    增加
     */
	public int insert(CustomerVehicle cv) throws CarsmartException {
		return cvManager.insert(cv);

	}
    /*
    按车辆id删除
     */
	public void deleteByVehicleId(Long vehicleId) throws CarsmartException {
		CustomerVehicle cv = new CustomerVehicle();
		cv.setVehicleId(vehicleId);
		cvManager.delete(cv);

	}
    /*
    按车辆id和顾客id删除
     */
	public void deleteByCV(Long customerId, Long vehicleId) throws CarsmartException {
		CustomerVehicle cv = new CustomerVehicle();
		cv.setCustomerId(customerId);
		cv.setVehicleId(vehicleId);
		cvManager.delete(cv);

	}

	public List<CustomerVehicle> getByCustomerId(Long customerId, Boolean owner) {
		CustomerVehicle cv = new CustomerVehicle();
		cv.setCustomerId(customerId);
		if (owner != null) {
			cv.setOwner(owner);
		}
		List<CustomerVehicle> list = cvManager.listByExample(cv);
		if (list == null || list.isEmpty()) {
			return null;
		}

		return list;
	}

	public List<CustomerVehicle> getByVehicleId(Long vehicleId, Boolean owner) {
		CustomerVehicle cv = new CustomerVehicle();
		cv.setVehicleId(vehicleId);
		if (owner != null) {
			cv.setOwner(owner);
		}
		List<CustomerVehicle> list = cvManager.listByExample(cv);
		if (list == null || list.isEmpty()) {
			return null;
		}

		return list;
	}

	public List<CustomerVehicle> getByVIdList(List<Long> vList, Boolean owner) {
		BaseExample example = cvManager.getExample();
		Criteria c = example.or();
		c.andPropertyIn(vList, "vehicleId");
		if(owner != null){
			c.andPropertyEqualTo(owner, "owner");
		}
		List<CustomerVehicle> list = cvManager.listByBaseExample(example);
		if(list == null || list.isEmpty()){
			return null;
		}
		return list;
	}
	/*
	更新
	 */
	public void update(CustomerVehicle update) throws CarsmartException {
		cvManager.update(update);

	}

}
